Graphics Class

Graphics class objects are used for drawing text, lines, rectangles, ovals, and pictures. Normally you use a graphics object in response to a Paint event, but you can also perform direct drawing by using the Graphics property of a Canvas control.

Events

None

Properties

Bold

LastPage

TextHeight

Copies

PenHeight

TextSize

FirstPage

PenWidth

Underline

ForeColor

Pixel

UseOldRenderer

Height

TextAscent

Width

Italic

TextFont

 

Methods

ClearRect

DrawPolygon

FillRect

DrawCautionIcon

DrawRect

FillRoundRect

DrawLine

DrawRoundRect

NextPage

DrawNoteIcon

DrawStopIcon

StringDirection

DrawObject

DrawString

StringHeight

DrawOval

FillOval

StringWidth

DrawPicture

FillPolygon

 

More information available in parent classes: Object

Alternatively, you can create vector graphics using the subclasses of the Object2D class. You cannot create a meaningful Graphics object via the New command.

The X and Y parameters of the following methods are the horizontal and vertical coordinates of the left-top corner of the object being drawn or cleared. The origin (0,0) is the top-left corner of the control or window in which the drawing is being done. For example 50,50 is 50 pixels to the right and 50 pixels down from the top-left of the window or control.


Notes

By default, REALbasic uses the Quartz graphics engine on Mac OS X when you call methods of the Graphics class such as DrawLine, DrawRect, DrawOval, and so forth. The Quartz graphics engine uses anti-aliasing to make lines look smoother. The disadvantage is that it is slower than line drawing using the older Apple technology. For most applications, the Quartz engine will be fast enough and it does produce more attractive results on Mac OS X. However, if you need more speed, you can improve the performance of the drawing methods under Mac OS X by turning off the Quartz graphics engine and using the QuickDraw engine instead. Do this by setting the UseOldRenderer property of the Graphics class to True before you begin drawing.


Examples

This example uses the Paint event handler of a Canvas control to draw the text "The quick brown fox" in Helvetica bold, italic, 18 point, 50 pixels from the top of and 10 pixels from the left side of the control.

Sub Paint(g As Graphics)
 g.Bold= True
 g.Italic= True
 g.TextFont="Helvetica"
 g.TextSize=18
 g.DrawString "The quick brown fox", 10, 50

This example assigns the color of a particular pixel in a Canvas control to a variable.

Dim c as Color
c=Canvas1.Graphics.Pixel(10,10)

This example sets a particular pixel of a Canvas control to a specific RGB value.

Canvas1.Graphics.Pixel(10,10)= RGB(100,105,225)

This is example draws a triangle in a Canvas control. It is placed in the Paint event. The parameter g as Graphics is passed into this event.

Dim Points(6) as Integer
Points(1)=10  //X of Point 1
Points(2)=10  //Y of Point 1
Points(3)=75  //X of Point 2
Points(4)=30  //Y of Point 2
Points(5)=10  //X of Point 3
Points(6)=125 //Y of Point 3
g.ForeColor= RGB(100,200,255)
g.FillPolygon Points

The following example assumes that you have imported a resource file into the Project Editor that contains a PICT resource whose ID is 129. The following line of code in the Canvas control's Open event handler assigns the picture to the control's Backdrop property:

Me.backdrop=app.resourceFork.getpicture(129)

Note: Access to the resourcefork is supported only on Macintosh. Check the value of the TargetMacOS constant before attempting to access the resourcefork.


See Also

Canvas control.